#! /bin/bash

TARGET_DIR="/home/ubuntu/tti_astrbot"

echo "Attempting to change directory to ${TARGET_DIR}..."
cd "${TARGET_DIR}"
if [ $? -ne 0 ]; then
    echo "Error: Could not change directory to ${TARGET_DIR}. Current directory is $(pwd)." >&2
    exit 1 # 发生错误，退出脚本
fi
echo "Successfully changed directory to $(pwd)."

echo "Activating virtual environment..."
source venv/bin/activate
if [ $? -ne 0 ]; then
    echo "Error: Could not activate virtual environment. Make sure venv/bin/activate exists and is executable." >&2
    exit 1
fi
echo "Virtual environment activated."

echo "Starting python application..."
nohup python main.py > t2i.log 2>&1 &
if [ $? -ne 0 ]; then
    echo "Error: Failed to start python application." >&2
else
    echo "Python application started in background. Logging to t2i.log."
    echo "Process ID: $!" > pid.log  # $! 是最后一个后台进程的PID
    echo "Process ID: $!"
fi

